<template>
  <div>
    <el-upload :action="importFileUrl" name="importfile"  :file-list="fileList" :onError="uploadError" :onSuccess="uploadSuccess" :on-remove="uploadRemove"
     :before-upload="uploadBefore">
      <el-button size="small" type="primary">上传文件</el-button>
    </el-upload>
  </div>
</template>

<script>
export default {
  props: ["fileList", "fileinfo"],
  data() {
    return {
      importFileUrl: "",
    };
  },
  created() {
    this.importFileUrl = localStorage.getItem("baseURL") + "/api/MediaCenter/PostFile";
  },
  methods: {
    //上传成功
    uploadSuccess: function(response, file, fileList) {
      let aa = this.fileinfo;
      let obj = { name: file.name, url: response.data.fileUrls };
      this.fileinfo.push(obj);
      this.$emit("uploadCallback", this.fileinfo);
    },

    // 上传错误
    uploadError: function(response, file, fileList) {
      this.$message({
        message: "上传失败，请重试",
        type: "warning"
      });
    },

    //删除
    uploadRemove: function(file, fileList) {
      this.fileinfo.forEach((item, index) => {
        if (item.name == file.name) {
          this.fileinfo.splice(index, 1);
        }
      });
      this.$emit("uploadCallback", this.fileinfo);
    },

    //上传之前格式检测
    uploadBefore: function(file) {
     
    },
   
  }
};
</script>
